Syllabus, Last Week, Book and This Week
R - Script, Notebook and Markdown
GitHub
PRACTICE and QUIZ
Next Week
Extended Syllabus PDF
Week 4 - Presentation LINK
<-#
(Pg. 22-27)
Scientific Calculator
Class
Vectors
Matrices
Arrays
?Arithmetic or help(“Arithmetic”)
example(integer) , demo(graphics)
R> 2+3
[1] 5
R> 14/6
[1] 2.333333
R> 14/6+5
[1] 7.333333
R> 14/(6+5)
[1] 1.272727
R> 3^2
[1] 9
R> 2^3
[1] 8
R> sqrt(x=9)
[1] 3
R> sqrt(x=5.311)
[1] 2.304561
R> f <- expression(x^2+3*x) # you can check ?expression
R> D(f,'x') # Calculate (first) derivative of f
# with respect to x
2 * x + 3
Variables are defined with different data types
Also
Variables are assigned with R-Objects
—> The data type of the R-object
Any number with (or without) a decimal point.
a <- 3.8
a
class(a)
b <- 4
b
class(b)
c <- sqrt(2)
c
class(c)
d <- 3.5:9.5
d
class(d)
class(1)
Kind of a sub-class of the numeric class.
The suffix L tells R to store this as an integer.
a <- 7
a
class(a)
b <- 7L
b
class(b)
c <- 5:9
c
class(c)
d <- 5.1:9.1
d
class(d)
class(3.2L)
pi
sqrt(2)^2-2
Numeric (64-bit) -> big memery and calculations
Integer (32-bit) -> Constant lalues like ID
6 digits after decimal
16 significant digits
Complex: x2 = −1 (imaginary number)
a <- i
b <- 1i
b
class(b)
class(1+2i)
class(2iL)
try
class(((1i^2)^2))
is.complex((1i^2)^2)
isTRUE(is.complex((1i^2)^2))
(1i^2)^2
TRUE or FALSE - Logical Operators
5 < 9
5 < -9
a <- 5 < -9
class(a)
1:10
1:10 >= 5
x <- 1:10 >= 5
1:10 < 2
y <- 1:10 < 2
x | y
z <- x | y
z
!z
class(z)
b <- 4:8
b
c <- 7:11
c
b != c
d <- 5:12
b != d
Data type consists of letters or words. String.
single quotes: ’ … ’ or double quotes " … "
name <- emir
name <- 'emir'
name
class(name)
a <- 23
a
class(a)
b <- '23'
b
class(b)
print('hello')
cat('hello')
class(print('hello'))
class(cat("hello"))
Null, Infinity, Not a Number, Not Available
NULL # Null (“empty” entity)
Inf # Infinity
class(Inf)
Inf*-9
is.finite(Inf)
1/0
NaN # Not a Number
class(NaN)
-Inf+Inf
is.nan(5^(-Inf/Inf))
0/0
NA # Not Available (“missing” entity)
class(NA)
Sys.Date( )
date()
date <- "2007-06-22"
class(date)
date1 <- as.Date(date) # Coercion
class(b)
date2 <- as.Date("2004-02-13")
date1 - date2
date_difference <- date1 - date2
class(date_difference)
%d day as a number (0-31) 01-31
%a abbreviated weekday Mon
%A unabbreviated weekday Monday
%m month (00-12) 00-12
%b abbreviated month Jan
%B unabbreviated month January
%y 2-digit year 07
%Y 4-digit year 2007
today <- Sys.Date()
format(today, format="%B %d %Y")
3
class(3)
as.numeric(3)
as.character(3)
as.logical(3)
FALSE
class(FALSE)
as.character(FALSE)
as.numeric(FALSE)
as.numeric(TRUE)
TRUE+TRUE
class(TRUE+TRUE)
The simplest data structure in R
Vectors are a list-like structure that contain items of the same data type.
spring_month <- "April"
spring_month
spring_months <- c("March", "April","May","June")
spring_months
class(spring_months)
c means “combine”
myvec <- c(1, 3, 1, 42)
a <- 35
myvec2 <- c(3L, 3.45, 1e+03, 64^0.5, 2+(3-1.1)/9.44, a)
myvec3 <- c(myvec, myvec2)
myvec3
x <- c("all", "b", "olive")
Length of a vector, length(vector_name)
length(x)
Indexing element, vector_name[element_position]
x[2]
Manipulating element of vector, assigning arrow
x[2] <- "b_new"
x
Note: In R, counting elements start position 1, not 0.
y <- c( 1.2, 5, "Rt", "2000", 20, 4905)
y [0]
class(y)
y
Sequences
7:16.4
a <- 7:16
a
seq(from=7,to=16,by=3)
seq(50,150,25)
seq(50,149,25)
seq(from=3,to=27,length.out=40)
Round
3/2
round(3/2)
round(5.1)
round(pi)
Repetition
rep(x=1, times=4)
rep(x=c(3, 62, 8),times=3)
rep(x=c(3, 62, 8),times=3,each=2)
Sorting
sort(x=c(2.5, -1, -10, 3.44)) # decreasing=FALSE (default)
sort(x=c(2.5, -1,- 10, 3.44), decreasing=TRUE)
Random - Uniform Distribution
runif(15, min = 20, max = 45)
runif(15, 20, 45)
runif(25, 60, 50)
Random variable can be saved
set.seed(1)
runif(15, 20, 45)
Vectors indexed using two indices instead of one.
n <- runif(9,1,100)
n
matrix(n, nrow = 3, ncol = 3)
n2 <- runif(10,1,100)
matrix(n2, nrow = 3, ncol = 3)
x <- as.numeric(seq(10,120,10))
mx <- matrix(x,3,4) # n, nrow, ncol
mx
class(mx)
class(mx[1])
typeof(mx)
mx[1,]
mx[,2]
mx[,2:4]
mx_new <- mx[,2:4]
mx
mx[2,3] <- "rose"
mx
class(mx)
typeof(mx)
mx_new <- as.numeric(mx)
mx_new
class(mx_new)
typeof(mx_new)
m_mycol <- matrix(c(1, 2, 3, 4, 5, 6),
nrow = 2,
ncol = 3,
byrow = FALSE) # Default
m_mycol
m_byrow <- matrix(c(1, 2, 3, 4, 5, 6),
nrow = 2,
ncol = 3,
byrow = TRUE)
m_byrow
t(m_byrow)
length(mx)
dim(mx)
x <- 1:24
x
array(x, dim = c(4,3,2)) # raw, col, level
arr <- array(x, c(4,3,2))
class(arr)
typeof(arr)
arr <- array(data=10:33,dim=c(3,4,2))
arr
arr[2,2,1]
arr[-1,,]
Scientific Calculator
Scientific Calculator
Problem: Compute double, triple or higher order integrals
install.packages("cubature")
library(cubature)
f <- function(x) 1
adaptIntegrate(f,lowerLimit = c(0,0,0),upperLimit = c(4,4,4))
$integral
[1] XX
Create a Function
Problem: Take a sample belonged to population and sum
pop <- 1:6 # This is my population
samp <- sample(pop, size = 2) # This is my sample, I choose two var.
sum(samp)
pop
samp
I want to create a new function named roll()
roll <- function() {
pop <- 1:6
samp <- sample(die, size = 2)
sum(samp)
}
roll()
Create a Function
Problem: What if we removed one line of code from our function and changed the name pop to box ?
roll2 <- function() {
samp <- sample(box, size = 2)
sum(samp)
}
roll2()
Re-create function
roll2 <- function(box) {
samp <- sample(box, size = 2)
sum(samp)
}
roll2(box = 1:4)
roll2(box = 1:6)
roll2(1:20)
Create a Function
print()
List - Data Frame - Attributes - ATMOS
Udemy – Introduction to R, Section 3 (Video 22-25)
List and Data Frame